package com.bluesky.jwf.component.window;

import com.bluesky.javawebbrowser.domain.html.tags.Tag;
import com.bluesky.jwf.ComponentFactory;
import com.bluesky.jwf.component.Component;
import com.bluesky.jwf.component.HyperLinkButton;
import com.bluesky.jwf.component.event.AbstractEventHandler;
import com.bluesky.jwf.component.event.EventHandler;

/**
 * a title field and a close button
 * 
 * @author jack
 * 
 */
public class WindowHead extends Component {

	private String title;
	private Window window;

	private Tag titleTag;
	private Tag closeButtonTag;

	@Override
	public void init(String html) {

		super.init(html);

		titleTag = loadByJwfClass("title");
		closeButtonTag = loadByJwfClass("closeButton");

		HyperLinkButton btn = (HyperLinkButton) ComponentFactory.getInstance()
				.createComponent(HyperLinkButton.class.getSimpleName());
		btn.setText("[X]");
		btn.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter,
					EventHandler onComplete) {
				if (window != null)
					window.close();

			}
		});
		closeButtonTag.replace(btn);
	}

	public Window getWindow() {
		return window;
	}

	public void setWindow(Window window) {
		this.window = window;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
		titleTag.setBodyAndParse(title);
	}

}
